package com.shixi.shixitraining.shixitraining.ui.tabmine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.event.LoginEvent;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.LoginBean;
import com.shixi.shixitraining.shixitraining.util.Base64Utils;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private List<String> countryCodeList;
    private EditText mName;
    private EditText mPassWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EventBus.getDefault().isRegistered(this);
        initTile();
    }

    private void initTile() {

        TextView mRegister = (TextView) findViewById(R.id.register_tv);
        ImageView mClose = (ImageView) findViewById(R.id.login_close);
        TextView mLogin = (TextView) findViewById(R.id.login_tv);
        mName = (EditText) findViewById(R.id.name_edt);
        mPassWord = (EditText) findViewById(R.id.psw_edt);
        mLogin.setOnClickListener(this);
        mClose.setOnClickListener(this);
        mRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_close:
                killActivity();
                break;
            case R.id.register_tv:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.login_tv:
                LoginRequest();
                break;


        }
    }

    private void LoginRequest() {

        final String name = mName.getText().toString();
        String password = mPassWord.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showShortToast(this, "用户名不能为空");
            return;
        } else if (TextUtils.isEmpty(password)) {
            ToastUtil.showShortToast(this, "密码不能为空");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("password",  Base64Utils.getBase64(password));

        HttpUtil.getInstance().getApiInterface().login(map).enqueue(new CallbackImple<LoginBean>() {
            @Override
            public void onError(Call<LoginBean> call, Throwable t) {
                ToastUtil.showShortToast(LoginActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<LoginBean> call, LoginBean response) {
                if (response != null) {
                    if (response.getStatus() == 200 && response.getData() != null) {
                        String token = response.getData().getToken();
                        int user_id = response.getData().getUserId();
                        String userName = response.getData().getUserName();
                        Log.e("token",token);
                        String userId = String.valueOf(user_id);
                        UserManager.getInstance().saveToken(token);
                        UserManager.getInstance().saveUserId(userId);
                        UserManager.getInstance().saveUserName(userName);
                        EventBus.getDefault().post(new LoginEvent(response.getData().getUserName()));
                        finish();
                    } else {
                        ToastUtil.showShortToast(LoginActivity.this, response.getMsg());
                    }
                } else {
                    ToastUtil.showShortToast(LoginActivity.this, "数据为空");
                }
            }
        });

    }
//    private void CountryCodeSelect() {
//        String [] CountryArray = mPhoneCode.getResources().getStringArray(R.array.country_arrays);
//        String [] CodeArray = mPhoneCode.getResources().getStringArray(R.array.code_arrays);
//        countryCodeList = new ArrayList<>();
//        for (int i=0;i<CountryArray.length;i++){
//            countryCodeList.add(CountryArray[i]+"("+CodeArray[i]+")");
//        }
//
//        OptionsPickerView options=new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                    mPhoneCode.setText(countryCodeList.get(options1));
//            }
//        }).build();
//        options.setPicker(countryCodeList);
//        options.show();
//    }

    private void killActivity() {
        this.finish();
        //必须在finish后调用
        this.overridePendingTransition(R.anim.activity_stay, R.anim.activity_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
